<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="所属分销商" prop="dtName">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择所属分销商"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属店铺" prop="changePriceStore">
        <el-select
          v-model.number="dataForm.storeId"
          placeholder="点击选择店铺"
          @focus="storeChange"
          ><el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option
        ></el-select>
      </el-form-item>
      <el-form-item label="价格区间始" prop="changePriceStart">
        <el-input
          v-model.number="dataForm.changePriceStart"
          placeholder="价格区间始"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格区间止" prop="changePriceEnd">
        <el-input
          v-model.number="dataForm.changePriceEnd"
          placeholder="价格区间止"
        ></el-input>
      </el-form-item>
      <el-form-item label="加价百分比" prop="chargePricePercent">
        <el-input
          v-model.number="dataForm.chargePricePercent"
          placeholder="加价百分比"
        ></el-input>
      </el-form-item>
      <el-form-item label="加价额" prop="chargePriceAmount">
        <el-input
          v-model.number="dataForm.chargePriceAmount"
          placeholder="加价额"
        ></el-input>
      </el-form-item>
      <el-form-item label="最高价" prop="highestPrice">
        <el-input v-model.number="dataForm.highestPrice" placeholder="最高价"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        dtId: "",
        dtName: "",
        storeId: "",
        storeName: "",
        chargePriceType: "",
        changePriceStart: "",
        changePriceEnd: "",
        chargePricePercent: "",
        chargePriceAmount: "",
        highestPrice: "",
      },
      stores: [],
      dtList: [],
      dtListTreeProps: {
        label: "name",
        children: "children",
      },
      dataRule: {
        dtName: [{ required: true, message: "分销商不能为空", trigger: "blur" }],
        storeId: [{ required: true, message: "店铺不能为空", trigger: "blur" }],
        chargePriceType: [
          { required: true, message: "加价类型不能为空", trigger: "blur" },
        ],
        changePriceStart: [
          { required: true, message: "价格区间始不能为空", trigger: "blur" },
        ],
        changePriceEnd: [
          { required: true, message: "价格区间止不能为空", trigger: "blur" },
        ],
        chargePricePercent: [
          { required: true, message: "加价百分比不能为空", trigger: "blur" },
        ],
        chargePriceAmount: [
          { required: true, message: "加价额不能为空", trigger: "blur" },
        ],
        highestPrice: [{ required: true, message: "最高价不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.dtId = data.id;
      this.dataForm.dtName = data.name;
    },
    // 分销商设置当前选中节点
    dtListListTreeSetCurrentNode() {
      this.$refs.dtTree.setCurrentKey(this.dataForm.dtId);
      this.dataForm.dtName = (this.$refs.dtTree.getCurrentNode() || {})["name"];
    },
    init(id) {
      // 获取分销商下拉树
      this.$http({
        url: this.$http.adornUrl("/op/commprice/listdt"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 100000,
        }),
      }).then(({ data }) => {
        this.dtList = treeDataTranslate(data.page.list, "id", "parentId");
      });

      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/op/commprice/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.storeId = data.commPrice.storeId;
              this.dataForm.dtId = data.commPrice.dtId;
              this.dataForm.chargePriceType = data.commPrice.chargePriceType;
              this.dataForm.changePriceStart = data.commPrice.changePriceStart;
              this.dataForm.changePriceEnd = data.commPrice.changePriceEnd;
              this.dataForm.chargePricePercent = data.commPrice.chargePricePercent;
              this.dataForm.chargePriceAmount = data.commPrice.chargePriceAmount;
              this.dataForm.highestPrice = data.commPrice.highestPrice;
              this.dtListListTreeSetCurrentNode();
            }
          });
        }
      });
    },

    // 点击触发店铺选择
    storeChange() {
      this.$http({
        url: this.$http.adornUrl("/op/group/store"),
        method: "get",
        params: this.$http.adornParams({
          dtId: this.dataForm.dtId
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stores = data.list;
        }
      });
    },


    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/op/commprice/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              dtId: this.dataForm.dtId,
              storeId: this.dataForm.storeId,
              chargePriceType: this.dataForm.chargePriceType,
              changePriceStart: this.dataForm.changePriceStart,
              changePriceEnd: this.dataForm.changePriceEnd,
              chargePricePercent: this.dataForm.chargePricePercent,
              chargePriceAmount: this.dataForm.chargePriceAmount,
              highestPrice: this.dataForm.highestPrice,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
